package com.book.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.book.business.entity.BookCart;
import com.book.business.entity.BookLove;
import com.book.business.entity.BookSell;
import com.book.business.service.BookCartService;
import com.book.business.service.BookIndentService;
import com.book.business.service.BookLoveService;
import com.book.business.service.BookSellService;
import com.book.buyout.service.PrelistingService;
import com.book.common.annotation.ControllerEndpoint;
import com.book.common.annotation.Log;
import com.book.common.controller.BaseController;
import com.book.common.entity.FebsConstant;
import com.book.common.entity.FebsResponse;
import com.book.common.entity.QueryRequest;
import com.book.common.utils.FebsUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @Author: 盛佳帅
 * @Description:  20173953
 */

@Controller
public class BookLoveController extends BaseController {
    @Autowired
    BookLoveService bookLoveService;
    @Autowired
    PrelistingService prelistingService;
    @Autowired
    BookIndentService bookIndentService;
    @Autowired
    BookCartService bookCartService;
    @Autowired
    BookSellService bookSellService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "business/bookLove")
    public String bookLoveIndex() {
        return FebsUtil.view("business/love/bookLoveList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "business/bookLove/pay/{id}")
    public String bookLoveUpdate(@PathVariable String id, ModelMap modelMap) {
        BookLove bookLove = bookLoveService.getById(id);
        modelMap.put("bookLove", bookLove);
        return FebsUtil.view("business/love/bookLoveToCart");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "business/bookLove/update/{id}")
    public String bookLovePay(@PathVariable Integer id, ModelMap modelMap) {
        BookLove bookLove = this.bookLoveService.getById(id);
        modelMap.put("bookLove", bookLove);
        return FebsUtil.view("business/love/bookLoveUpdate");
    }

    @GetMapping("bookLove")
    @ResponseBody
    @RequiresPermissions("bookLove:view")
    public FebsResponse bookLoveList(BookLove bookLove) {
        return new FebsResponse().success().data(this.bookLoveService.findBookLove(bookLove));
    }

    @Log("访问收藏夹")
    @GetMapping("bookLove/list")
    @ResponseBody
    @RequiresPermissions("bookLove:view")
    public FebsResponse bookLoveIPage(QueryRequest request, BookLove bookLove) {
        Map<String, Object> dataTable = getDataTable(this.bookLoveService.pageBookLove(request, bookLove));
        return new FebsResponse().success().data(dataTable);
    }

    @Log("收藏夹-删除")
    @PostMapping("bookLove/delete/{ids}")
    @ResponseBody
    @RequiresPermissions("bookLove:delete")
    @ControllerEndpoint(operation = "删除书籍信息成功", exceptionMessage = "删除书籍信息失败")
    public FebsResponse bookLovedelete(@PathVariable String ids) {
        String[] attr = ids.split(",");
        List<BookLove> bookLoveList = bookLoveService.payBookLove(attr);
        for (int i = 0; i < bookLoveList.size(); i++) {
            QueryWrapper<BookSell> sellQueryWrapper = new QueryWrapper<>();
            sellQueryWrapper.eq("bookname", bookLoveList.get(i).getBookname())
                    .eq("pubid", bookLoveList.get(i).getPubid());
            BookSell bookSell = bookSellService.getOne(sellQueryWrapper);
            if (bookSell != null) {
                bookSell.setLove(0);
                bookSellService.updateBookSell(bookSell);
            }
        }
        this.bookLoveService.deleteBookLove(attr);
        return new FebsResponse().success();
    }

    @Log("收藏夹-加入购物车")
    @PostMapping("bookLove/cart")
    @ResponseBody
    @RequiresPermissions("bookLove:cart")
    @ControllerEndpoint(operation = "加入购物车成功", exceptionMessage = "加入购物车失败")
    public FebsResponse bookLove(@Valid BookLove bookLove) {
        QueryWrapper<BookSell> sellQueryWrapper = new QueryWrapper<>();
        sellQueryWrapper.eq("bookname", bookLove.getBookname())
                .eq("pubid", bookLove.getPubid());
        BookSell bookSell = bookSellService.getOne(sellQueryWrapper);
        if (bookSell == null) {
            return new FebsResponse().message("非常抱歉，该书籍已下架！");
        }
        QueryWrapper<BookCart> bookCartQueryWrapper = new QueryWrapper<>();
        bookCartQueryWrapper.eq("bookname", bookLove.getBookname())
                .eq("pubid", bookLove.getPubid());
        BookCart bookCart = bookCartService.getOne(bookCartQueryWrapper);
        if (bookCart == null) {
            bookCart = new BookCart()
                    .setUsername(getCurrentUser().getUsername())
                    .setBookname(bookLove.getBookname())
                    .setBookmoney(bookLove.getBookmoney())
                    .setPubid(bookLove.getPubid())
                    .setMessage(bookLove.getMessage())
                    .setType(bookLove.getType())
                    .setNumber(bookLove.getNumber());
            bookCartService.addBookCart(bookCart);
        } else {
            bookCart.setNumber(bookCart.getNumber() + bookLove.getNumber());
            bookCartService.updateBookCart(bookCart);
        }
        return new FebsResponse().success();
    }
}

